#include <boost/algorithm/string.hpp>
#include <boost/functional/hash.hpp>

typedef std::pair<int,Entry> INT_ENTRY_PAIR;

struct Bucket2 {
	int key;
	Bucket2 * prev,* next;
	INT_ENTRY_MAP * elements;

	Bucket2(int _key, Bucket2 *_prev, Bucket2 *_next ) {
		elements = new INT_ENTRY_MAP;
		key = _key;
		prev = _prev;
		next = _next;
	}

	~Bucket2(){
		//delete elements;
		//cerr << "destructor Bucket: " << key  << endl;
	}

	void add(Entry e) {
		//prinf("add: has entry: %d\n", (this->elements.find(e.hash) != this->elements.end()));
		(*elements).insert(INT_ENTRY_PAIR(e.hash, e));
		//prinf("My #el : %d\n", elements.size());
	}

	void remove(Entry e) {
		INT_ENTRY_MAP::iterator it = (*elements).find(e.hash);
		//prinf("remove: has entry: %d\n", (it != this->elements.end()));
		elements->erase(it->first);
	}

	bool has_elements() {
		return (*elements).size() > 0;
	}

	bool operator<(const Bucket2& rhs) {
			return key < rhs.key;
	}

	bool operator==(const Bucket2& rhs) {
		return key == rhs.key;
	}

	size_t hashvalue() const {
	    boost::hash<int> hasher;
	    return hasher(key);
	}

	string to_string() {
		stringstream ss;
		INT_ENTRY_MAP::iterator it = (*elements).begin();
		int _prev = prev != NULL ? (*prev).key : -1;
		int _next = next != NULL ? (*next).key : -1;
		ss << "bucket " << key << ": " << _prev << ":" << _next <<  ":";
		for (; it != (*elements).end(); it++) {
			ss << it->second.to_string() << " ";
		}
		return ss.str();
	}
};

